package com.example.springbootshiro.config;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.springbootshiro.mapper.PermsMapper;
import com.example.springbootshiro.pojo.Perms;
import com.example.springbootshiro.realm.UserRealm;
import com.example.springbootshiro.service.IPermsService;
import com.example.springbootshiro.service.impl.PermsServiceImpl;
import org.apache.shiro.mgt.DefaultSecurityManager;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Lazy;
import org.springframework.context.annotation.Primary;
import org.springframework.util.CollectionUtils;

import java.util.*;

/**
 * <p>
 *
 * </p>
 *
 * @author gqz
 * @version 1.0
 * @since 2021/9/2:16:01
 */
@Configuration
public class ShiroConfig {

    @Autowired
    @Lazy
    private IPermsService permsService;

    //ShiroFilterFactoryBean:shiro过滤对象
   @Bean
   public ShiroFilterFactoryBean shiroFilterFactoryBean(@Qualifier("getDefaultSecurityManager") DefaultSecurityManager defaultSecurityManager)
   {
       ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
       //设置安全管理器
       shiroFilterFactoryBean.setSecurityManager(defaultSecurityManager);
       /**
        * 添加shiro的内置过滤器
        * anno:无需认证就能访问
        * authc:必须认证了才能访问
        * user:必须拥有记住我功能才能访问
        * perms:必须拥有对某个资源的权限才能访问
        * roles:必须拥有某个角色才能访问
        */

       LinkedHashMap<String, String> filterMap = new LinkedHashMap<>();
       List<Perms> perms = permsService.findAll();
       if (!CollectionUtils.isEmpty(perms))
       {
           perms.forEach(
                   p->{
                       if (Optional.ofNullable(p.getMenu()).isPresent())
                       {
                           //拦截
                           filterMap.put(p.getMenu(),"authc");
                       }
                       if (Optional.ofNullable(p.getMenu()).isPresent()&&Optional.of(p.getOperate()).isPresent())
                       {
                           //授权,正常情况下,用户如果未授权,会跳转到未授权页面
                           filterMap.put(p.getMenu(),"perms["+p.getOperate()+"]");
                       }
                   }
           );
       }

       //设置过滤器链
       shiroFilterFactoryBean.setFilterChainDefinitionMap(filterMap);
       //设置登陆页面
       shiroFilterFactoryBean.setLoginUrl("/shiro/loginPage");
       //设置未授权页面
       shiroFilterFactoryBean.setUnauthorizedUrl("/shiro/noauth");
       return shiroFilterFactoryBean;
   }


    //DefaultWebSecurityManager:shiro安全对象
    @Bean
    public DefaultWebSecurityManager getDefaultSecurityManager(@Qualifier("getUserRealm") UserRealm userRealm)
    {
        DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
        //关联userRealm
        securityManager.setRealm(userRealm);
        return securityManager;
    }

    //创建Realm对象,需要自定义
    @Bean
    public UserRealm getUserRealm()
    {
        return new UserRealm();
    }
}
